home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / rcs5ap1s.lzh / RCSGEN.C < prev    next >
C/C++ Source or Header  |  1991-01-30  |  12KB  |  393 lines

  1. /*
  2.  *                     RCS revision generation
  3.  */
  4.  
  5. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  6.    Copyright 1990 by Paul Eggert
  7.    Distributed under license by the Free Software Foundation, Inc.
  8.  
  9. This file is part of RCS.
  10.  
  11. RCS is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 1, or (at your option)
  14. any later version.
  15.  
  16. RCS is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with RCS; see the file COPYING.  If not, write to
  23. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. Report problems and direct all questions to:
  26.  
  27.     rcs-bugs@cs.purdue.edu
  28.  
  29. */
  30.  
  31.  
  32.  
  33. /* $Log: rcsgen.c,v $
  34.  * Revision 5.7  1991/01/30  14:21:32  apratt
  35.  * CI with RCS version 5
  36.  *
  37.  * Revision 5.6  90/12/27  19:54:26  eggert
  38.  * checked in with -k by apratt at 91.01.10.13.15.18.
  39.  * 
  40.  * Revision 5.6  1990/12/27  19:54:26  eggert
  41.  * Fix bug: rcs -t inserted \n, making RCS file grow.
  42.  *
  43.  * Revision 5.5  1990/12/04  05:18:45  eggert
  44.  * Use -I for prompts and -q for diagnostics.
  45.  *
  46.  * Revision 5.4  1990/11/01  05:03:47  eggert
  47.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  48.  *
  49.  * Revision 5.3  1990/09/21  06:12:43  hammer
  50.  * made putdesc() treat stdin the same whether or not it was from a terminal
  51.  * by making it recognize that a single '.' was then end of the
  52.  * description always
  53.  *
  54.  * Revision 5.2  1990/09/04  08:02:25  eggert
  55.  * Fix `co -p1.1 -ko' bug.  Standardize yes-or-no procedure.
  56.  *
  57.  * Revision 5.1  1990/08/29  07:14:01  eggert
  58.  * Clean old log messages too.
  59.  *
  60.  * Revision 5.0  1990/08/22  08:12:52  eggert
  61.  * Remove compile-time limits; use malloc instead.
  62.  * Ansify and Posixate.
  63.  *
  64.  * Revision 4.7  89/05/01  15:12:49  narten
  65.  * changed copyright header to reflect current distribution rules
  66.  * 
  67.  * Revision 4.6  88/08/28  14:59:10  eggert
  68.  * Shrink stdio code size; allow cc -R; remove lint; isatty() -> ttystdin()
  69.  * 
  70.  * Revision 4.5  87/12/18  11:43:25  narten
  71.  * additional lint cleanups, and a bug fix from the 4.3BSD version that
  72.  * keeps "ci" from sticking a '\377' into the description if you run it
  73.  * with a zero-length file as the description. (Guy Harris)
  74.  * 
  75.  * Revision 4.4  87/10/18  10:35:10  narten
  76.  * Updating version numbers. Changes relative to 1.1 actually relative to
  77.  * 4.2
  78.  * 
  79.  * Revision 1.3  87/09/24  13:59:51  narten
  80.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  81.  * warnings)
  82.  * 
  83.  * Revision 1.2  87/03/27  14:22:27  jenkins
  84.  * Port to suns
  85.  * 
  86.  * Revision 4.2  83/12/02  23:01:39  wft
  87.  * merged 4.1 and 3.3.1.1 (clearerr(stdin)).
  88.  * 
  89.  * Revision 4.1  83/05/10  16:03:33  wft
  90.  * Changed putamin() to abort if trying to reread redirected stdin.
  91.  * Fixed getdesc() to output a prompt on initial newline.
  92.  * 
  93.  * Revision 3.3.1.1  83/10/19  04:21:51  lepreau
  94.  * Added clearerr(stdin) for re-reading description from stdin.
  95.  * 
  96.  * Revision 3.3  82/11/28  21:36:49  wft
  97.  * 4.2 prerelease
  98.  * 
  99.  * Revision 3.3  82/11/28  21:36:49  wft
  100.  * Replaced ferror() followed by fclose() with ffclose().
  101.  * Putdesc() now suppresses the prompts if stdin
  102.  * is not a terminal. A pointer to the current log message is now
  103.  * inserted into the corresponding delta, rather than leaving it in a
  104.  * global variable.
  105.  *
  106.  * Revision 3.2  82/10/18  21:11:26  wft
  107.  * I added checks for write errors during editing, and improved
  108.  * the prompt on putdesc().
  109.  *
  110.  * Revision 3.1  82/10/13  15:55:09  wft
  111.  * corrected type of variables assigned to by getc (char --> int)
  112.  */
  113.  
  114.  
  115.  
  116.  
  117. #include "rcsbase.h"
  118.  
  119. libId(genId, "$Id: rcsgen.c,v 5.7 1991/01/30 14:21:32 apratt Exp $")
  120.  
  121. int interactiveflag;  /* Should we act as if stdin is a tty?  */
  122. struct cbuf curlogmsg;  /* buffer for current log message */
  123. struct buf curlogbuf;  /* same, including allocated but unused bytes */
  124.  
  125. enum stringwork {copy, edit, expand, edit_expand };
  126.  
  127. static void scandeltatext P((struct hshentry*,enum stringwork));
  128.  
  129.  
  130.  
  131.  
  132.     const char *
  133. buildrevision(deltas, target, tostdout, expandflag)
  134.     const struct hshentries *deltas;
  135.     struct hshentry *target;
  136.     int tostdout;
  137.     int expandflag;
  138. /* Function: Generates the revision given by target
  139.  * by retrieving all deltas given by parameter deltas and combining them.
  140.  * If tostdout is set, the revision is printed on the standard output,
  141.  * otherwise written into a temporary file.
  142.  * Temporary files are put into tmp unless the caller has already set up
  143.  * the temp file directory by invoking initeditfiles(), which sets fcopy.
  144.  * if expandflag is set, keyword expansion is performed.
  145.  * Returns nil on errors, the name of the file with the revision otherwise.
  146.  *
  147.  * Algorithm: Copy initial revision unchanged.  Then edit all revisions but
  148.  * the last one into it, alternating input and output files (resultfile and
  149.  * editfile). The last revision is then edited in, performing simultaneous
  150.  * keyword substitution (this saves one extra pass).
  151.  * All this simplifies if only one revision needs to be generated,
  152.  * or no keyword expansion is necessary, or if output goes to stdout.
  153.  */
  154. {
  155.     static const char nonnil[] = ""; /* some char* value that isn't nil */
  156.  
  157.     if (deltas->first == target) {
  158.                 /* only latest revision to generate */
  159.         if (tostdout) {
  160.                         fcopy=stdout;
  161.             scandeltatext(target, expandflag?expand:copy);
  162.             return nonnil;
  163.                 } else {
  164.             if (!fcopy)
  165.                 inittmpeditfiles();
  166.                         scandeltatext(target,expandflag?expand:copy);
  167.                         ffclose(fcopy);
  168.                         return(resultfile);
  169.                 }
  170.         } else {
  171.                 /* several revisions to generate */
  172.         if (!fcopy)
  173.             inittmpeditfiles();
  174.                 /* write initial revision into fcopy, no keyword expansion */
  175.         scandeltatext(deltas->first, copy);
  176.         while ((deltas=deltas->rest)->rest) {
  177.                         /* do all deltas except last one */
  178.             scandeltatext(deltas->first, edit);
  179.                 }
  180.         if (expandflag | tostdout) {
  181.                         /* first, get to beginning of file*/
  182.             finishedit((struct hshentry *)nil);
  183.             swapeditfiles(tostdout);
  184.                 }
  185.         scandeltatext(deltas->first, expandflag ? edit_expand : edit);
  186.         finishedit(expandflag ? deltas->first : (struct hshentry*)nil);
  187.         if (tostdout)
  188.             return nonnil;
  189.         ffclose(fcopy);
  190.         return resultfile;
  191.         }
  192. }
  193.  
  194.  
  195.  
  196.     static void
  197. scandeltatext(delta,func)
  198. struct hshentry * delta; enum stringwork func;
  199. /* Function: Scans delta text nodes up to and including the one given
  200.  * by delta. For the one given by delta, the log message is saved into
  201.  * curlogmsg and the text is processed according to parameter func.
  202.  * Assumes the initial lexeme must be read in first.
  203.  * Does not advance nexttok after it is finished.
  204.  */
  205. {
  206.     const struct hshentry *nextdelta;
  207.     struct cbuf cb;
  208.  
  209.         for (;;) {
  210.                 nextlex();
  211.                 if (!(nextdelta=getnum())) {
  212.             fatserror("can't find delta for revision %s", delta->num);
  213.                 }
  214.         getkeystring(Klog);
  215.         if (delta==nextdelta) {
  216.             cb = savestring(&curlogbuf);
  217.             delta->log = curlogmsg =
  218.                 cleanlogmsg(curlogbuf.string, cb.size);
  219.                 } else {readstring();
  220.                 }
  221.                 nextlex();
  222.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  223.             ignorephrase();
  224.         getkeystring(Ktext);
  225.  
  226.         if (delta==nextdelta)
  227.             break;
  228.         readstring(); /* skip over it */
  229.  
  230.     }
  231.     switch (func) {
  232.         case copy: copystring(); break;
  233.         case expand: xpandstring(delta); break;
  234.         case edit: editstring((struct hshentry *)nil); break;
  235.         case edit_expand: editstring(delta); break;
  236.     }
  237. }
  238.  
  239.     struct cbuf
  240. cleanlogmsg(m, s)
  241.     char *m;
  242.     size_t s;
  243. {
  244.     register char *t = m;
  245.     register const char *f =